package com.gzhryc.shared_device.oem.micro.api;

import com.gzhryc.common.ClassTools;
import com.gzhryc.common.IDTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.shared_device.oem.code.member.LoginMember;
import com.gzhryc.shared_device.oem.code.member.LoginMemberSessionManager;
import com.gzhryc.shared_device.oem.code.member.dao.db.Member;
import com.gzhryc.shared_device.oem.code.member.services.MemberService;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.AliLoginCheckResponse;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.WxLoginCheckResponse;

public class TestUtils {

    static Logger log = Logger.getLogger(TestUtils.class);

    public static boolean testLogin(WxLoginCheckResponse apiResp){
        if (MicroConstants.isDebug() >= 2) {
            //测试账号
            Member member = MemberService.self().getById(9101L);
            if (member != null) {
                String loginToken = IDTools.getUUID();
                LoginMember loginMember = ClassTools.copy(new LoginMember(), member);
                loginMember.setLoginToken(loginToken);
                if (LoginMemberSessionManager.self().setMemberSession(loginMember)) {
                    MemberService.self().login(member.getId());
                    String resDomain = PropertyTools.get().getString(MicroConstants.ResourceDomain);
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setMemberId(member.getId().toString());
                    apiResp.getBody().setMemberName(member.getNickname());
                    apiResp.getBody().setMemberFaceImg(WebUtils.setHttpDomain(resDomain, member.getFaceImg()));
                    apiResp.getBody().setLoginToken(loginToken);

                    if (StringTools.isNotBlank(member.getPhone())) {
                        apiResp.getBody().setMemberPhone(member.getPhone());
                        apiResp.getBody().setLoginLevel(3);
                    } else if (MicroConstants.MemberDefaultName.equals(member.getNickname())) {
                        apiResp.getBody().setLoginLevel(1);
                    } else {
                        apiResp.getBody().setLoginLevel(2);
                    }
                    return true;
                } else {
                    log.error("缓存会员信息失败，会员信息：{{0:json}}", member);
                }
            }
        }
        return false;
    }
    
    public static boolean testLogin(AliLoginCheckResponse apiResp){
        if (MicroConstants.isDebug() >= 2) {
            //测试账号
            Member member = MemberService.self().getById(9101L);
            if (member != null) {
                String loginToken = IDTools.getUUID();
                LoginMember loginMember = ClassTools.copy(new LoginMember(), member);
                loginMember.setLoginToken(loginToken);
                if (LoginMemberSessionManager.self().setMemberSession(loginMember)) {
                    MemberService.self().login(member.getId());
                    String resDomain = PropertyTools.get().getString(MicroConstants.ResourceDomain);
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setMemberId(member.getId().toString());
                    apiResp.getBody().setMemberName(member.getNickname());
                    apiResp.getBody().setMemberFaceImg(WebUtils.setHttpDomain(resDomain, member.getFaceImg()));
                    apiResp.getBody().setLoginToken(loginToken);

                    if (StringTools.isNotBlank(member.getPhone())) {
                        apiResp.getBody().setMemberPhone(member.getPhone());
                        apiResp.getBody().setLoginLevel(3);
                    } else if (MicroConstants.MemberDefaultName.equals(member.getNickname())) {
                        apiResp.getBody().setLoginLevel(1);
                    } else {
                        apiResp.getBody().setLoginLevel(2);
                    }
                    return true;
                } else {
                    log.error("缓存会员信息失败，会员信息：{{0:json}}", member);
                }
            }
        }
        return false;
    }
}
